/**
 * 
 */
package truerefactor.metrics;

import java.util.List;

import truerefactor.graph.ClassNode;
import truerefactor.graph.CodeGraph;

/**
 * NOC - An implementation of the Number of Children Metric.
 * 
 * @author Isaac Griffith
 */
public class NOC extends Metric {

    /**
     * Creates a new instance of an NOC Metric
     */
    public NOC()
    {
        this.shortName = "NOC";
        this.name = "Number of Children";
        this.description = "";
    }

    /*
     * (non-Javadoc)
     * @see truerefactor.metrics.Metric#measure(truerefactor.graph.CodeGraph)
     */
    @Override
    public double measure(CodeGraph graph)
    {
        double aNOC = 0;
        double tNOC = 0;
        double cNOC = 0;

        // NOC = the number of subclasses of a given class
        // cNOC is the Number of Children for a given class
        // tNOC is the total sum of NOC for all classes
        // aNOC is the average NOC for the entire hierarchy.
        List<ClassNode> classes = graph.getClasses();
        for (ClassNode cNode : classes)
        {
            cNOC = measure(cNode);
            tNOC += cNOC;
        }

        if (classes.size() > 0)
            aNOC = tNOC / classes.size();

        return aNOC;
    }

    /**
     * Calculates the number of children for the the given ClassNode
     * 
     * @param cNode
     *            class node which requires measurement
     * @return number of children for the given classNode
     */
    public static int measure(ClassNode cNode)
    {
        return cNode.getSubClasses().size();
    }
}
